/* client.c — 简单的 TCP 客户端 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "protocol.h"    // ← 协议接口
#include "common.h"

#define SERVER_IP   "192.168.193.91"  // 服务器 IP
#define SERVER_PORT 8888         // 服务器端口
#define BUF_SIZE    4096         // 缓冲区大小
#define DATA_CHUNK  1024   // 每次发包时的数据大小





int recv_parse_execute(int sock_fd) {
    uint8_t  buffer[BUF_SIZE];
    ssize_t  n = recv(sock_fd, buffer, BUF_SIZE, 0);
    if (n < 0) {
        perror("recv");
        return -1;
    }
    if (n == 0) {
        // 对端关闭
        fprintf(stderr, "connection closed by peer\n");
        return -1;
    }

    uint16_t msgType, dataLen;
    uint8_t  data[BUF_SIZE];
    int ret = parse_packet(&msgType, data, &dataLen, buffer, (size_t)n);
    if (ret <= 0) {
        fprintf(stderr, "parse_packet error=%d\n", ret);
        return -2;
    }

    if (msgType != 0x0001) {
        fprintf(stderr, "unexpected msgType=0x%04X\n", msgType);
        return -3;
    }

    // 打印调试
    char *hex = show_hex(data, dataLen);
    printf("%s", hex);

    // 只取前两位，保证 "01" / "02"
    char cmd_str[3] = { hex[0], hex[1], '\0' };
    free(hex);

    // 解析命令
    int cmd = (int)strtol(cmd_str, NULL, 16);
    switch (cmd) {
        case 1:
            printf("发送录音指令\n");
            // TODO: send_record_command(sock_fd);
            break;
        case 2:
            printf("start send_file\n");
            //send_file(file_name);
            break;
        default:
            printf("unknown cmd[%d]\n", cmd);
            break;
    }

    // 回一个 ACK
    {
        uint8_t ackBuf[BUF_SIZE];
        const char *ack = "OK";
        int ackLen = build_packet(0x8001,
                                  (const uint8_t*)ack,
                                  strlen(ack),
                                  ackBuf,
                                  BUF_SIZE);
        if (ackLen > 0) {
            send(sock_fd, ackBuf, ackLen, 0);
        }
    }

    return 0;
}



int main(int argc, char *argv[]) {

    char buffer[BUF_SIZE];
    int sock_fd;
    struct sockaddr_in serv_addr;
    char send_buf[BUF_SIZE], recv_buf[BUF_SIZE];

    // 1. 创建套接字
    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    // 2. 设定服务器地址
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SERVER_PORT);
    if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
        perror("inet_pton");
        close(sock_fd);
        exit(EXIT_FAILURE);
    }

    // 3. 连接服务器
    if (connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("connect");
        close(sock_fd);
        exit(EXIT_FAILURE);
    }

    int ret = recv_parse_execute(sock_fd);

    
    // 5. 关闭套接字
    close(sock_fd);
    return 0;
}
